package com.jml.loadbalancer.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import com.jml.loadbalancer.LoadBalancer;
import com.jml.loadbalancer.entity.WeightEntity;


public class WeightLoadBalancer implements LoadBalancer<WeightEntity> {

    private final List<String> addresList = new ArrayList<String>();
    private AtomicLong atomicLong = new AtomicLong(0);

    private int index;
    
    public WeightLoadBalancer(List<WeightEntity> addres) {
        for (int i = 0; i < addres.size(); i++) {
            WeightEntity weightEntity = addres.get(i);
            int weight = weightEntity.getWeight();
            for (int j = 0; j < weight; j++) {
                addresList.add(addres.get(i).getAddres());
            }
        }
    }
    
    public synchronized String getSingleAddres(List<WeightEntity> addres) {
        // 对权重数据实现重新排序
        if (index >= addresList.size()) {
            index = 0;
        }
        String ads = addresList.get(Integer.parseInt(index++ + ""));
        System.out.println("ads1:" + ads);
        return ads;
    }
}
